package com.uyaer.vo
{
	import com.uyaer.components.BodyBox;
	
	import flash.geom.Rectangle;

	[Bindable]
	public class LevelVo
	{
		/**
		 *唯一编码符号 
		 */		
		public var uuid:String;
		/**
		 *关卡名称 
		 */		
		public var name:String;
		/**
		 *关卡数据 
		 */		
		public var bodies:Vector.<BodyBox> = new Vector.<BodyBox>();
		
		public function LevelVo(name:String)
		{
			this.name = name;
			this.uuid = new Date().time.toString(16)+Math.round(Math.random()*10000).toString(16);
		}
		
		public function jsonToVo(obj:Object):void{
			this.name = obj.name;
			bodies = new Vector.<BodyBox>();
			var arr:Array = obj.bodies;
			var box:BodyBox;
			for each (var bo:Object in arr) 
			{
				box = new BodyBox();
				box.nameid = bo.name;
				box.bodyType = bo.bodyType;
				box.shapeType = bo.shapeType;
				box.x = bo.x-box.getBodyOffset().x;
				box.y = bo.y-box.getBodyOffset().y;
				box.setBodyBounds(bo.x,bo.y,bo.w,bo.h,false);
				bodies.push(box);
			}
			
		}
		
		public function toJson():Object{
			var json:Object = {"name":name};
			var bs:Array = new Array();
			for each (var b:BodyBox in bodies) 
			{
				var bounds:Rectangle = b.getBodyBounds();
				var bo:Object = {
					"name":b.nameid,
					"bodyType":b.bodyType,
					"shapeType":b.shapeType,
					"x":bounds.x,
					"y":bounds.y,
					"w":bounds.width,
					"h":bounds.height
				};
				bs.push(bo);
			}
			json.bodies = bs;
			
			return json;
		}
		
		public function toAs3():String{
			var str:String = "public static const "+name+":Object = "+JSON.stringify(toJson());
			return str;
		}
	}
}